﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineAuction.Business;
using OnlineAuction.Entity;

namespace OnlineAuction.Admin
{
    public partial class EditCategory : System.Web.UI.Page
    {
        private string _categoryLogo = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null
                || Request.Cookies["UserInfo"]["Role"].ToString().CompareTo("Admin") != 0)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                CategoryBO cbo = new CategoryBO();
                CategoryE ce = cbo.GetCategoryForID(
                    Convert.ToInt32(Request.QueryString["CategoryID"].ToString()));
                txtCategoryName.Text = ce.CategoryName;
                if (ce.CategoryState == false)
                {
                    rbtState.SelectedValue = "false";
                }
                else
                {
                    rbtState.SelectedValue = "true";
                }
                _categoryLogo = ce.CategoryLogo;
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            reset();
        }

        private void reset()
        {
            txtCategoryName.Text = "";
            if (CategoryLogo.HasFile == true)
            {
                CategoryLogo.FileName.Remove(0);
            }
        }

        //Add new item
        protected void btnUpdateCategory_Click(object sender, EventArgs e)
        {
            CategoryBO cbo = new CategoryBO();
            string path = Server.MapPath("~/Image1/");
            if (CategoryLogo.HasFile == true)
            {
                CategoryLogo.SaveAs(path + CategoryLogo.FileName);
                _categoryLogo = "Image1/" + CategoryLogo.FileName;
            }
            //create new category entity
            CategoryE category = new CategoryE(
                Convert.ToInt32(Request.QueryString["CategoryID"].ToString()),
                txtCategoryName.Text, _categoryLogo, bool.Parse(rbtState.SelectedValue));
            //Check result add item
            bool flag = cbo.UpdateCategory(category);
            if (flag)
            {
                Response.Write("<script>alert('Success');</script>");
                Response.Redirect("ViewCategory.aspx");
            }
            else
            {
                Response.Write("<script>alert('Error');</script>");
            }
        }
    }
}